<script setup lang="ts">
import { ref, reactive, onMounted, watch, nextTick } from "vue";
import { useRoute } from "vue-router";
import { router } from "@/router";
import { tabsList } from "./helps";

import ThreatIntelligenceGraph from "./components/threatIntelligenceGraph.vue";
import AssetLibraryGraph from "./components/assetLibraryGraph.vue";
import VulnerabilityCorrelationGraph from "./components/vulnerabilityCorrelationGraph.vue";

const route = useRoute();
const activeTab = ref("0");

// 获取URL参数中的task
const taskParam = ref((route.query.task as string) || "");

// 子组件引用
const threatIntelligenceGraphRef =
  ref<InstanceType<typeof ThreatIntelligenceGraph>>();
const assetLibraryGraphRef = ref<InstanceType<typeof AssetLibraryGraph>>();
const vulnerabilityCorrelationGraphRef =
  ref<InstanceType<typeof VulnerabilityCorrelationGraph>>();

// 处理 tab 切换
const handleTabChange = async (tabName: string) => {
  // 等待 DOM 更新完成
  await nextTick();
  // 延迟调用 resize，确保容器已经显示
  setTimeout(() => {
    switch (tabName) {
      case "0":
        threatIntelligenceGraphRef.value?.resizeChart?.();
        break;
      case "1":
        assetLibraryGraphRef.value?.resizeChart?.();
        break;
      case "2":
        vulnerabilityCorrelationGraphRef.value?.resizeChart?.();
        break;
    }
  }, 150);
};
</script>

<template>
  <div>
    <el-tabs
      type="border-card"
      v-model="activeTab"
      @tab-change="handleTabChange"
    >
      <el-tab-pane label="威胁情报汇聚图谱" value="0">
        <ThreatIntelligenceGraph
          ref="threatIntelligenceGraphRef"
          :task="taskParam"
        />
      </el-tab-pane>
      <el-tab-pane label="资产库汇聚图谱" value="1">
        <AssetLibraryGraph ref="assetLibraryGraphRef" :task="taskParam" />
      </el-tab-pane>
      <el-tab-pane label="脆弱性关联图谱" value="2">
        <VulnerabilityCorrelationGraph
          ref="vulnerabilityCorrelationGraphRef"
          :task="taskParam"
        />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<style lang="scss" scoped>
.search-form-container {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.search-actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
}

.el-btn-zese {
  background: #722ed1;
  color: white;
  border-color: #722ed1;
}

.el-btn-zese.is-disabled,
.el-btn-zese.is-disabled:focus,
.el-btn-zese.is-disabled:hover {
  background: #722ed1;
  color: white;
  border-color: #722ed1;
}
.task-name {
  font-size: 14px;
  font-weight: bold;
  margin-bottom: 10px;
  border: 1px solid #e0e0e0;
  padding: 10px;
  border-radius: 5px;
}
</style>
